k-tree
E-learning book

Time series analysis

In the examples in this article, data is generated every time the page loads. If you want to see an example with different values - reload the page.

online tool

.

Components of the time series

When analyzing a time series, three components are distinguished: trend, seasonality and noise. A trend is a general trend, seasonality, as the name implies, the effects of periodicity (day of the week, time of year, etc.) and, finally, noise are random factors.

In order to understand the difference between these three quantities, we will simulate the function of the distance from the earth to the moon. It is known that on average the moon every year it moves away by 4 cm - this is a trend, during the day the moon rotates around the earth and the distance fluctuates from ~362600 km to ~405400 km is seasonality. Noise is "random" factors, for example, the influence of other planets. If we draw the sum of these three graphs, then we get a time series - a function showing the change in distance from the earth to the moon in time.

Chart 1. Trend
Graph 2. Seasonality
Graph 3. Noise

Trend. Smoothing methods

Smoothing methods are necessary to remove noise from the time series. There are various ways of smoothing, the main ones are the moving average method and the exponential smoothing method.

Moving average method

The idea of the moving average method is to shift the chart point by the average value of a certain interval. An odd number of sections are taken as an interval, for example, three - the previous, current and next periods, the average is found and is taken as a smoothed value:

Si = Σkj=-k(xi+j)/(2k+1)

This method has a problem: a random high or low value strongly affects the moving line. As weights were introduced to the solutions. Window functions are used for weight distribution, the main window functions are the Dirichlet window (rectangular function), B-splines, polynomials, sinusoidal and cosine:

Graph 4. Gann window for n=5 (cosine window)
Graph 5. Sinusoidal window for n=5

 

The disadvantages of using a moving average are the complexity of calculations and incorrect data at the ends of the chart.

Source dataMoving AverageWeighted moving average (sinusoidal window, n=5)Weighted moving average (Gann window, n=5)
800 854 283 0
908 845 481 400
828 902 515 454
1070 1032 557 414
1320 1141 647 535
1345 1198 757 660
1058 1325 757 673
1577 1567 781 529
2288 1611 986 789
1520 1889 1119 1144
2170 1736 1168 760
965 1613 982 1085
1796 1768 942 483
2139 2034 998 898
3236 2454 1424 1070
2645 2269 1655 1618
1056 1934 1420 1323
800 1969 873 528
3374 2348 983 400
4163 2779 2294 1687
Table 1. Moving average smoothing
Chart 6. Smoothing of the moving average. Red is the initial data, peach is the moving average, yellow and ochre are the moving average weighted by a sinusoidal window with n=7 and n=3, respectively

 

As can be seen from the graph, an increase in n gives a smoother function, thus leveling out smaller fluctuations in the time series. Note that when smoothing, it does not matter whether the average graph matches the data graph or not, the goal is to build the correct form.

Exponential smoothing method

The exponential smoothing method got its name because the smoothed function exponentially decreases the influence of the previous period with a certain sensitivity coefficient. The smoothed value is found as the difference between the previous valid value and calculated value:

D't = α·Dt-1 + (1-α)·D't-1

The sensitivity coefficient, α, is selected between 0 and 1, the value 0.3 is used as the basis. If there is a sufficient sample, then the coefficient is selected by optimization.

Source dataExponential smoothing, α=0.1Exponential smoothing, α=0.6
800 800 800
908 -640 160
828 667 481
1070 -518 304
1320 573 520
1345 -384 584
1058 480 573
1577 -326 406
2288 451 784
1520 -177 1059
2170 311 488
965 -63 1107
1796 153 136
2139 42 1023
3236 176 874
2645 165 1592
1056 116 950
800 1 254
3374 79 378
4163 266 1873
Table 2. Exponential smoothing
Graph 7. Exponential smoothing with α=0.1 (peach line) and α=0.6 (yellow line)

Forecasting methods

Forecasting methods are based on identifying trends in a time series and then using the found value to predict future values. In forecasting methods, trend and seasonality are distinguished, in general, all types of seasonality can be found by successive iterations. For example, when analyzing data for a year, you can highlight the seasonality of the time of year, and in the remaining the trend is to find seasonality by days of the week and so on.

Double exponential smoothing

Double exponential smoothing produces a smoothed level and trend value.

Attention! There may be confusion, the Holt-Winters method differs in terms: trend, seasonality and noise the level, trend and seasonality are called respectively.

Smooth - smoothing, smoothed level for the period τ, sτ, depends on the value of the level for the current period (Dτ), the trend for the previous period (tτ-1) and the calculated smoothed value for the previous period (sτ-1):
sτ = αDτ + (1 - α)(sτ-1 + tτ-1)
Trend - trend, trend for the period τ, tτ, depends on the calculated smoothed value for the previous and current periods (sτ and sτ-1) and from the previous trend:
tτ = β(sτ-sτ-1) + (1-β)tτ-1
The level and trend calculated according to these formulas can be used in forecasting:
D'τ+h = sτ + h·tτ

When calculating, the values of s and t for the first period are assigned s1 = D1 and t=0

Graph 8. Data (peach line), exponential smoothing - level (yellow line), trend (ochre line) and forecast (black line) by exponential smoothing

Holt-Winters method

The Holt-Winters method includes a seasonal component, i.e. periodicity. There are two varieties of the method - multiplicative and additive. Unlike double exponential smoothing, the Holt-Winters method also studies the influence of periodicity.

The general idea of finding the values of the smoothed level, trend and periodicity is as follows: the smoothed level (s - smooth, sometimes l - level is used) is the base level of values, the trend (t - trend) is an indicator of the growth rate, the difference between the smoothed values of the current and previous period. To study the periodicity (p - period), we divide the data into periods of size k and highlight the influence of each element (1,2,...,k) of the period on smoothed level.

For more accurate calculations, a feedback indicator is introduced.

In a general sense, feedback is the influence of previous values for new ones: for example, when you start talking, you adjust the volume of your voice depending on what your ears hear - this is feedback.

To start the calculations, the values of s, t and k, in the simplest form, can be selected as sτ = Dτ, t = 0, p = 0.

k is the length of the selected period:
sτ = α(Dτ - pτ-k) + (1 - α)(sτ-1 + tτ-1)
Adjusted for previous values of tτ-k (feedback)
tτ = β(sτ-sτ-1) + (1-β)tτ-1
Adjusted for previous values of pτ-k (feedback)
pτ = γ(Dτ - sτ) + (1-γ)pτ-k

The following formula is used for forecasting:

xτ+h = Dτ + htτ + pτ-k+h

Holt-Winters multiplicative method

The multiplicative method differs from the additive one in that the parameters affecting the periodicity and the smoothed level calculated by the ratio:

pτ = γ(Dτ/sτ) + (1-γ)pτ-k
sτ = α(Dτ/pτ-k) + (1 - α)(sτ-1 + tτ-1)
tτ = β(sτ-sτ-1) + (1-β)tτ-1

The following formula is used for forecasting:

xτ+h = (Dτ + htτ)pτ-k+h

Holt-Winters method in excel

Table for downloading in formats ods and xls.

Prediction quality

Checking the quality of forecasting is possible if there is a sufficient sample and is an important test for reliability to check and optimize the values of α, β and γ, it is necessary to build a forecast on existing data, for example, if we have data for five years and we want to predict the next year, then it is necessary to build a model on the first four years, check and optimize the coefficients to minimize the error between forecast and data for the 5th year. After optimization the model can be rebuilt taking into account the last period to improve accuracy, followed by the construction of a forecast.

Optimization methods will be described in a separate article, below is an example of forecasting by the Holt Winters method.

Graph 9. Site traffic data for four weeks
#Data stp stp
193 9300 9300
291 92-0.1-0.5 92-0.10.99
372 84-0.89-6 84-0.890.93
475 80-1.2-2.5 80-1.20.97
575 77-1.38-1 77-1.380.99
657 68-2.14-5.5 68-2.140.92
766 66-2.130 66-2.131
8123 880.2817.5 38-4.721.62
985 870.15-1.25 54-2.651.28
1085 890.34-5 67-1.091.1
1191 910.51-1.25 770.021.08
12102 960.962.5 871.021.08
1373 900.26-11.25 850.720.89
1460 78-0.97-9 75-0.350.9
1599 79-0.7718.75 69-0.921.53
16108 910.517.88 75-0.231.36
1798 960.96-1.5 800.291.16
18104 1001.261.38 870.961.14
1983 930.43-3.75 840.561.03
2068 88-0.11-15.63 810.20.86
2162 81-0.8-14 76-0.320.86
2259 64-2.426.88 61-1.791.25
2380 66-1.9810.94 59-1.811.36
24121 870.3216.25 760.071.38
25112 971.298.19 850.961.23
2685 940.86-6.38 850.861.02
27106 1061.97-7.82 1012.370.95
2882 1031.47-17.5 1002.030.84
Graph 9. Example of predicting site traffic based on data for four weeks. The yellow line is the initial data, the red line is the forecast for the fifth a week based on the first four. The smoothed level line is filled in at α=0.4, β=0.1, γ=0.5
Download article in PDF format.

Do you find this article curious? /

Seen: 46 044

Rating: 5 (1 votes)

Read the following
The Monte Carlo method